package com.vegetable.modules.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.Query;
import com.vegetable.modules.entity.sys.SysLogEntity;
import com.vegetable.modules.entity.sys.po.EntityChangeLogQueryPO;
import com.vegetable.modules.mapper.sys.SysLogDao;
import com.vegetable.modules.service.sys.SysLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("sysLogService")
public class SysLogServiceImpl extends ServiceImpl<SysLogDao, SysLogEntity> implements SysLogService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");

        IPage<SysLogEntity> page = this.page(
            new Query<SysLogEntity>().getPage(params),
            new QueryWrapper<SysLogEntity>().like(StringUtils.isNotBlank(key),"username", key)
        );

        return new PageUtils(page);
    }
    
    /**
     * 查询实体变更日志
     */
    @Override
    public IPage<SysLogEntity> queryEntityChangeLogs(EntityChangeLogQueryPO po) {
        Page<SysLogEntity> page = new Page<>(po.getPage(), po.getLimit());
        
        QueryWrapper<SysLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("entity_class");
        
        if (StringUtils.isNotBlank(po.getEntityClass())) {
            queryWrapper.like("entity_class", po.getEntityClass());
        }
        
        if (StringUtils.isNotBlank(po.getOperationType())) {
            queryWrapper.eq("operation_type", po.getOperationType());
        }
        
        if (StringUtils.isNotBlank(po.getUsername())) {
            queryWrapper.like("username", po.getUsername());
        }
        
        queryWrapper.orderByDesc("create_date");
        
        return this.page(page, queryWrapper);
    }
}